<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    p{
      margin: 0;
      padding: 0;
    }
    .todo {
      width: 600px;
      padding: 20px;
      margin: 30px auto;
      border: 1px solid #666;
      border-radius: 10px;
    }
    .todo-header{
      display: flex;
    }
    .todo-header input{
      flex: 1;
    }
    .todo-body{
      margin-top: 20px;
    }
    .todo-body li{
      height: 40px;
      display: flex;
      align-items: center;
      border: 1px solid #dc6;
    }
    .todo-body li p {
      flex: 1;
    }
    .todo-body li div{
      margin: 0 10px;
    }
    .bg-done{
      background-color: rgb(125, 123, 123);
    }
    .bg-undone{
      background-color: rgb(173, 122, 122);
    }
    .ani-enter-from{
      transform: translateX(-100%);
    }
    .ani-leave-to{
      transform: translateX(100%);
    }
    .ani-enter-active, .ani-leave-active{
      transition: all 500ms;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="todo">
      <div class="todo-header">
        <input type="text" v-model.trim="inputTxt">
        <button @click="addTodo">确定</button>
      </div>
      <div class="todo-body">
        <ul>
          <li v-if="todos.length === 0">暂无代办事项</li>
          <transition-group name="ani">
            <li 
              v-for="(todo,index) in todos" 
              :class="{
                'bg-done': todo.done,
                'bg-undone': !todo.done
              }"
              :key="index">
              <p>{{todo.content}}</p>
              <div>
                <button @click="delTodo(index)">删除</button>
                <button @click="setTodoDone(index)" :disabled="todo.done">{{todo.done?'已完成':'点击已完成'}}</button>
              </div>
            </li>
          </transition-group>
        </ul>
      </div>
    </div>
  </div>
  <script src="./vue.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp({
      data() {
        return {
          todos: [
            {
              content: '明天去爬山',
              done: false
            },
            {
              content: '18号去钓鱼',
              done: true
            }
          ],
          inputTxt: ''
        }
      },
      methods: {
        setTodoDone (index) {
          // 设置待办事项已完成

          this.todos[index].done = true;
        },
        // 新增待办事项
        addTodo () {
          if(this.inputTxt === '') {
            return false;
          }
          this.todos.push({
            content: this.inputTxt,
            done: false
          })
          this.inputTxt = ''; // 清空value
        },
        // 删除待办事项
        delTodo(index) {
          if (confirm('您确认删除吗?')) {
            this.todos.splice(index, 1)
          }
        }
      }
    })
    app.mount('#app')
  </script>
</body>

</html>